﻿using HomeCloudStorageSystem.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HomeCloudStorageSystem.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class FileUploagLogsController : ControllerBase
    {
        private readonly IFileUploagLogRepository _fileUploagLogRepository;
        public FileUploagLogsController(IFileUploagLogRepository fileUploagLogRepository)
        {
            _fileUploagLogRepository = fileUploagLogRepository;
        }
        /// <summary>
        /// 展示文件上传记录的主子表的列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetFileUploadLogs()
        {
            var result = _fileUploagLogRepository.GetFileUploagLogs();
            if(result == null)
            {
                return BadRequest("错误的请求");
            }
            return new JsonResult(result);
        }
        /// <summary>
        /// 根据文件上传记录 ID 查看文件上传记录主子表信息
        /// </summary>
        /// <param name="id">文件上传记录 ID</param>
        /// <returns>文件上传记录主子表</returns>
        [HttpGet("{id}")]
        public IActionResult GetFileUploadLog(int id)
        {
            if(id <= 0 || string.IsNullOrEmpty(id.ToString()))
            {
                return BadRequest("请求错误");
            }
            if(!_fileUploagLogRepository.IsExist(id))
            {
                return BadRequest("错误的请求");
            }
            var result = _fileUploagLogRepository.GetFileUploagLog(id);
            return new JsonResult(result);
        }
    }
}
